package com.example.finaltest.pwd;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import android.content.Intent;
import com.example.finaltest.login.UserBean;
import com.example.finaltest.utils.SaveUserUtils;
import com.example.finaltest.MainActivity;
import com.example.finaltest.R;
import com.example.finaltest.utils.BaseActivity;
import com.example.finaltest.utils.SettingActivity;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

import static com.example.finaltest.utils.URLUtils.updatePwd_url;
import static com.example.finaltest.utils.URLUtils.verifyAnswer_url;

public class PwdchangeActivity extends BaseActivity {
    ImageView back;
    EditText et1, et2, et3;
    Button btn1;
    UserBean.DataBean userInfo, newInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pwdchange);
        userInfo = SaveUserUtils.getUserInfo(this); //获取到保存的登录信息
        initView();
        setListener();
        setView();
    }

    private void setView() {

    }

    private void setListener() {
        btn1.setOnClickListener(listener);
        back.setOnClickListener(listener);
    }

    private void initView() {
        et1 = findViewById(R.id.pwd_ch_et1);
        et2 = findViewById(R.id.pwd_ch_et2);
        btn1 = findViewById(R.id.pwd_ch_btn);
        back = findViewById(R.id.pwd_ch_back);
        et3 = findViewById(R.id.pwd_ch_et3);
    }

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int vid = v.getId();  // 获取到被点击的id
            switch (vid) {
                case R.id.pwd_ch_btn:
                    String pwd1, pwd2;
                    pwd1 = et2.getText().toString();
                    pwd2 = et3.getText().toString();
                    if (pwd1.equals(pwd2)) {
                        String oldPwd = et1.getText().toString();
                        Map<String, String> map = new HashMap<>();
                        map.put("userId", userInfo.getId() + "");
                        map.put("oldPwd", oldPwd);
                        map.put("newPwd", pwd1);
                        postNetwork(updatePwd_url, map);
                    } else {
                        Toast.makeText(PwdchangeActivity.this, "两次密码输入不一致！", Toast.LENGTH_SHORT).show();
                    }
                    break;
                case R.id.pwd_ch_back:
                    finish();
                    Intent intent = new Intent(PwdchangeActivity.this, SettingActivity.class);
                    startActivity(intent);
                    break;
            }
        }
    };

    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        // 解析数据
        UserBean bean = new Gson().fromJson(result, UserBean.class);
        int code = bean.getCode();
        switch (code) {
            case 200://获取成功
                Toast.makeText(PwdchangeActivity.this, "修改成功！", Toast.LENGTH_SHORT).show();
                SaveUserUtils.deleteUserInfo(PwdchangeActivity.this);
                Intent intent = new Intent(PwdchangeActivity.this, MainActivity.class);
                intent.putExtra("id", 2);
                startActivity(intent);
                Toast.makeText(PwdchangeActivity.this, "注销成功！请重新登陆！", Toast.LENGTH_SHORT).show();
                break;

            case 201:
                Toast.makeText(PwdchangeActivity.this, "原始密码错误！", Toast.LENGTH_SHORT).show();
                break;
            case 400:
                Toast.makeText(PwdchangeActivity.this, "参数错误！", Toast.LENGTH_SHORT).show();
                break;
            default:   //其他，登录失败
                Toast.makeText(PwdchangeActivity.this, "修改密码失败！", Toast.LENGTH_SHORT).show();
                break;
        }
    }
}
